﻿using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web.Mvc;
using ccgstore.WebApp.Models;
using ccgstore.WebApp.ViewModels;

namespace ccgstore.WebApp.Controllers
{
    public class CardController : ControllerBase
    {
        //
        // GET: /Card/

        public ActionResult Index()
        {
            var cards = Db.Cards.Include(c => c.Game);

            var vm = new CardVm();
            vm.SelectedGame = Db.Games.First(); // todo: store selected game somewhere
            vm.Cards = Db.Cards.Where(card => card.Game == vm.SelectedGame).ToList();

            return View(vm);
        }

        //
        // GET: /Card/Details/5

        public ActionResult Details(int id = 0)
        {
            Card card = Db.Cards.Find(id);
            if (card == null)
            {
                return HttpNotFound();
            }
            return View(card);
        }

        //
        // GET: /Card/Create
        [Authorize(Roles = "Administrator")]
        public ActionResult Create()
        {
            ViewBag.CardGameId = new SelectList(Db.Games, "GameId", "GameName");
            return View();
        }

        //
        // POST: /Card/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Card card)
        {
            if (ModelState.IsValid)
            {
                Db.Cards.Add(card);
                Db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.CardGameId = new SelectList(Db.Games, "GameId", "GameName", card.CardGameId);
            return View(card);
        }

        //
        // GET: /Card/Edit/5
        [Authorize]
        public ActionResult Edit(int id = 0)
        {
            Card card = Db.Cards.Find(id);
            if (card == null)
            {
                return HttpNotFound();
            }
            ViewBag.CardGameId = new SelectList(Db.Games, "GameId", "GameName", card.CardGameId);
            return View(card);
        }

        //
        // POST: /Card/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize]
        public ActionResult Edit(Card card)
        {
            if (ModelState.IsValid)
            {
                Db.Entry(card).State = EntityState.Modified;
                Db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CardGameId = new SelectList(Db.Games, "GameId", "GameName", card.CardGameId);
            return View(card);
        }

        //
        // GET: /Card/Delete/5
        [Authorize]
        public ActionResult Delete(int id = 0)
        {
            Card card = Db.Cards.Find(id);
            if (card == null)
            {
                return HttpNotFound();
            }
            return View(card);
        }

        //
        // POST: /Card/Delete/5
        [Authorize]
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Card card = Db.Cards.Find(id);
            Db.Cards.Remove(card);
            Db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            Db.Dispose();
            base.Dispose(disposing);
        }
    }
}